<?php

namespace catchAdmin\phone\model;

use catcher\exceptions\AlertErrorException;
use catcher\exceptions\MobileLoginErrorException;

class WechatUser extends \catchAdmin\model\WechatUser
{
    /**
     * 保存
     */
    public function saveData($param)
    {
        return $this->saveDataCC([
            'id'       => $this->getLoginId(),
            'avatar'   => $param['avatar'] ?? '',
            'nickname' => $param['nickname'] ?? '',
        ]);
    }


    /**
     * 保存父级
     */
    public function saveParent($param)
    {
        $parent_msg = $this
            ->where('share_code', $param['share_code'])
            ->find();
        if (empty($parent_msg)) {
            throw new AlertErrorException('分享码不存在');
        }

        $wechat_user = $this
            ->where('id', $this->getLoginId())
            ->find();
        if (empty($wechat_user['parent_id'])) {
            $wechat_user->parent_id = $parent_msg['id'];
            $wechat_user->save();
        }

        return 1;
    }


    ######################################## 显示 ################################################


    /**
     * 分享码查详情
     */
    public function readByShareCode($param)
    {
        $wechat_user_msg = $this
            ->where('share_code', $param['share_code'])
            ->find();
        if (empty($wechat_user_msg)) {
            throw new AlertErrorException('用户不存在');
        }

        return [
            'id'           => $wechat_user_msg->id,
            'share_code'   => $wechat_user_msg->share_code,
            'company_name' => $wechat_user_msg->company_name,
        ];
    }


    /**
     * 个人中心
     */
    public function getLoginInfo($param)
    {
        $msg = self::where('id', $this->getLoginId())
            ->find();
        if (empty($msg)) {
            throw new AlertErrorException('参数不正确');
        }

        $arr = $msg
            ->append([
                'notice_not_read_count',
                'need_count',
            ]);

        $arr['pay_can'] = 1;

        return $arr;
    }


    /**
     * 分享中心
     */
    public function getShareInfo($param)
    {
        $msg = self::where('id', $this->getLoginId())
            ->find();
        if (empty($msg)) {
            throw new AlertErrorException('参数不正确');
        }

        return $msg
            ->append([
                'all_income',
                'all_share_order',
                'all_user_num',
            ]);
    }


    /**
     * 收入中心
     */
    public function getIncomeInfo($param)
    {
        $msg = self::where('id', $this->getLoginId())
            ->find();
        if (empty($msg)) {
            throw new AlertErrorException('参数不正确');
        }

        $msg['withdraw_min'] = 100;

        return $msg
            ->append([
                'coming_income',
                'all_income',
                'all_withdraw',
            ]);
    }


    /**
     * 我的团队
     */
    public function indexSon($param)
    {
        return $this
            ->where('parent_id', $this->getLoginId())
            ->select()
            ->toArray();
    }


    public function checkLogin($param)
    {
        if (empty($this->getLoginId())) {
            throw new MobileLoginErrorException('未登陆');
        } else {
            return 1;
        }
    }

}
